//
//  SecondView.m
//  UI15_太平洋电脑网
//
//  Created by dllo on 16/3/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SecondView.h"
#import "Data.h"
#import "Model.h"
#import "Masonry.h"
#import "CollectionCell.h"
#import "UIImageView+WebCache.h"
#import "ButtonVCCell.h"
#import "ButtonView.h"
#import "LunBoTuView.h"
#import "ACCell.h"
#import "ThreeImageCell.h"
#import "NOImageCell.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT  self.view.frame.size.height
#define MAS_SHORTHAND          //这样子 `mas_width` 等就可以写成 `width`
#define MAS_SHORTHAND_GLOBALS  //这样子 `mas_equalTo` 和 `equalTo` 就没有区别了


@interface SecondView ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,retain)UIScrollView *scrollView;
@property (nonatomic,retain)UISegmentedControl *seg;

@property (nonatomic,retain)UITableView *squareTableView;  //广场
@property (nonatomic,retain)NSMutableArray *squareArray;

@property (nonatomic,retain)UICollectionView *ggCollection;
@property (nonatomic,retain)NSMutableArray *ggArray;  //广场轮播图

@property (nonatomic,retain)NSArray *buttonArray;
@property (nonatomic,retain)NSArray *titleArray;     //8个按钮

@property (nonatomic,retain)UICollectionView *activitrCol;
@property (nonatomic,retain)NSMutableArray *activityArray; //活动头条的数据

@end

@implementation SecondView
static NSString *const CollCell = @"collectioncell";
static NSString *const collCELL = @"cell";
static NSString *const acCell = @"acCell";
static NSString *const thirdCell = @"thirdCell";
static NSString *const noCell = @"noCell";
-(void)dealloc{
    [_scrollView release];
    [_seg release];
    [_squareTableView release];
    [_squareArray release];
    [_ggCollection release];
    [_ggArray release];
    [_buttonArray release];
    [_titleArray release];
    [_activitrCol release];
    [_activityArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createConfig];
    [self createData];
    [self createSquareTableView];  //广场页面
}

- (void)createConfig{
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:27 / 255.0 green:163 / 255.0 blue:217/255.0 alpha:1];
    self.navigationController.navigationBar.translucent = NO;
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 )];
    self.scrollView.backgroundColor = [UIColor grayColor];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(WIDTH * 3, 0);
    [self.view addSubview:self.scrollView];
    [_scrollView release];
#pragma mark - 三个标题
    self.seg = [[UISegmentedControl alloc]initWithItems:@[@"论坛广场",@"论坛列表",@"最近流浪"]];
    self.seg.frame = CGRectMake(0, 20, WIDTH, 30);
    self.navigationItem.titleView = self.seg;
    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    self.seg.tintColor = [UIColor clearColor];
    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20], NSForegroundColorAttributeName: [UIColor whiteColor]};
    [self.seg setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],  NSForegroundColorAttributeName: [UIColor lightTextColor]};
    [self.seg setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    self.seg.momentary = NO;   //点击可以返回原来的字体
    self.seg.layer.masksToBounds = NO;
    
}
// self.seg 点击方法
- (void)segAction:(UISegmentedControl *)segButton{
    switch (segButton.selectedSegmentIndex) {
        case 0:
            self.scrollView.contentOffset = CGPointMake(0, 0);
            break;
        case 1:
            self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
            break;
        case 2:
            self.scrollView.contentOffset = CGPointMake(WIDTH * 2, 0);
            break;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if ([scrollView isEqual:self.scrollView]) {
        self.seg.selectedSegmentIndex = scrollView.contentOffset.x / WIDTH;
    }else if([scrollView isEqual:self.ggCollection]){ //手动无线滑动
        if (self.ggCollection.contentOffset.x == WIDTH * 4 ) {
            self.ggCollection.contentOffset = CGPointMake(0, 0);
        }
    }else if ([scrollView isEqual:self.activitrCol]){
        if (self.activitrCol.contentOffset.y == 50*3 ) {
            self.activitrCol.contentOffset = CGPointMake(0, 0);
        }
    }
}

- (void)createData{
//广场轮播图和活动头条的数据
    [Data netWorkingWithURL:@"http://mrobot.pconline.com.cn/s-900/onlineinfo/bbs/forumFocus.xsp" block:^(id result) {
        NSDictionary *dic = result;
        NSArray *focusArr =dic[@"focus"];
        self.ggArray = [NSMutableArray array];
        for (NSDictionary *dic in focusArr) {
            Model *model = [[Model alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.ggArray addObject:model];
            [model release];
        }
        NSArray *activitysArr = dic[@"activitys"];
        self.activityArray = [NSMutableArray array];
        for (NSDictionary *dic in activitysArr) {
            Model *model = [[Model alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.activityArray addObject:model];
            [model release];
        }
        [self.activitrCol reloadData];
        [self.ggCollection reloadData];
        [self.squareTableView reloadData];
    }];
//tableView数据
    [Data netWorkingWithURL:@"http://mrobot.pconline.com.cn/s-900/onlineinfo/bbs/topics.xsp?type=hot_day&forums=769285,587012,769283,768253,250,721485,45,240027,671334,240201,681102,2,48,76,86&singleForum=false&noForums=240022&ie=utf-8&count=20&showImage=true&size=240x180" block:^(id result) {
        NSDictionary *dic = result;
        NSArray *array = dic[@"hot-topics"];
        self.squareArray = [[NSMutableArray alloc]init];
        for (NSDictionary *dic in array) {
            Model *model = [[Model alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.squareArray addObject:model];
            [model release];
        }
        [self.squareTableView reloadData];
    }];
}

- (void)createSquareTableView {
#pragma mark - 广场的tableView
    self.squareTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 736 - 64 - 49)];
        self.squareTableView.backgroundColor = [UIColor whiteColor];
    [ self.scrollView  addSubview:self.squareTableView];
    [_squareTableView release];
    self.squareTableView.contentInset =UIEdgeInsetsMake(450, 0, 0, 0);
    self.squareTableView.delegate =self;
    self.squareTableView.dataSource = self;
    self.squareTableView.rowHeight = 120;
    self.squareTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.squareTableView.showsHorizontalScrollIndicator = NO;  //隐藏水平滚动图
    self.squareTableView.showsVerticalScrollIndicator = NO;  //隐藏数值滚动图
    [self.squareTableView registerClass:[ThreeImageCell class] forCellReuseIdentifier:thirdCell];
    [self.squareTableView registerClass:[NOImageCell class] forCellReuseIdentifier:noCell];
#pragma mark - 广场的轮播图
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(WIDTH, 170);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    self.ggCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, -450, WIDTH, 170) collectionViewLayout:flowLayout];
    [self.squareTableView addSubview:self.ggCollection];
    [_ggCollection release];
    self.ggCollection.delegate = self;
    self.ggCollection.dataSource = self;
    self.ggCollection.pagingEnabled = YES;
    self.ggCollection.bounces = NO;
    [self.ggCollection registerClass:[CollectionCell class] forCellWithReuseIdentifier:CollCell];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, -280, 350, 30)];
    [self.squareTableView addSubview:label];
    label.tag = 2000;
    [label release];
    UILabel *number = [[UILabel alloc]initWithFrame:CGRectMake(350, -280, 414-350, 30)];
    [self.squareTableView addSubview:number];
    number.text = @"1/4";
    number.tag = 2001;
    [number release];
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changePic) userInfo:nil repeats:YES];
#pragma mark - 广场的8个图标
    UICollectionViewFlowLayout *eightOut = [[UICollectionViewFlowLayout alloc]init];
    eightOut.itemSize = CGSizeMake(90, 90);
    eightOut.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    eightOut.scrollDirection = UICollectionViewScrollDirectionVertical;
    eightOut.minimumLineSpacing = 0;
    UICollectionView *collView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, -250, WIDTH, 200) collectionViewLayout:eightOut];
    [self.squareTableView addSubview:collView];
    collView.backgroundColor = [UIColor whiteColor];
    collView.delegate = self;
    collView.scrollEnabled = NO;
    collView.dataSource = self;
    collView.tag = 1010;
    [collView release];
    [collView registerClass:[ButtonVCCell class] forCellWithReuseIdentifier:collCELL];
    self.buttonArray = @[@"1.png",@"2.png",@"3.png",@"4.png",@"5.png",@"6.png",@"7.png",@"8.png"];
    self.titleArray = @[@"手机",@"DIY",@"笔记本",@"摄影",@"Phone人院",@"二手",@"最数码",@"收藏"];
#pragma Mark - 广场的活动头条
    UILabel *aLable = [[UILabel alloc]initWithFrame:CGRectMake(10, -50, 120, 50)];
    aLable.backgroundColor = [UIColor yellowColor];
    aLable.text = @"活动头条";
    aLable.font = [UIFont systemFontOfSize:23];
    aLable.textAlignment = 1;
    aLable.textColor = [UIColor redColor];
    [self.squareTableView addSubview:aLable];
    [aLable release];
    
    UICollectionViewFlowLayout *acFlowLaout = [[UICollectionViewFlowLayout alloc]init];
    acFlowLaout.itemSize = CGSizeMake(274, 50);
    acFlowLaout.minimumLineSpacing = 0; //每行之间的间距为0
    self.activitrCol = [[UICollectionView alloc]initWithFrame:CGRectMake(130, -50, 274, 50) collectionViewLayout:acFlowLaout];
    [self.squareTableView addSubview:self.activitrCol];
    self.activitrCol.backgroundColor = [UIColor whiteColor];
    self.activitrCol.delegate = self;
    self.activitrCol.dataSource = self;
    self.activitrCol.showsVerticalScrollIndicator = NO;
    self.activitrCol.bounces = NO;
    self.activitrCol.pagingEnabled = YES;
    self.activitrCol.contentOffset = CGPointMake(0, 50);
    [_activitrCol release];
    [self.activitrCol registerClass:[ACCell class] forCellWithReuseIdentifier:acCell];
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(acChangePic) userInfo:nil repeats:YES];
}

- (void)changePic{
    [self.ggCollection setContentOffset:CGPointMake(self.ggCollection.contentOffset.x + WIDTH, 0) animated:YES];
    if (self.ggCollection.contentOffset.x == WIDTH * 4) {
        self.ggCollection.contentOffset = CGPointMake(0, 0);
    }
}

- (void)acChangePic{
    [self.activitrCol setContentOffset:CGPointMake(0,self.activitrCol.contentOffset.y + 50) animated:YES];
    if (self.activitrCol.contentOffset.y == 50 * 3) {
        self.activitrCol.contentOffset = CGPointMake(0, 0);
    }

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([collectionView isEqual:self.ggCollection]) {
        return 5;
    }else if ([collectionView isEqual:self.activitrCol]){
        return 4;
    }
    return 8;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([collectionView isEqual:self.ggCollection]) {
        CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollCell forIndexPath:indexPath];
        if (indexPath.row < 4) {
            Model *model = self.ggArray[indexPath.row];
            [cell.imageView  sd_setImageWithURL:[NSURL URLWithString:model.image]];
            UILabel *lable = (UILabel *)[self.view viewWithTag:2000];
            lable.text = model.title;
            UILabel *numberLable =  (UILabel *)[self.view viewWithTag:2001];
            numberLable.text = [NSString stringWithFormat:@"%ld/4",indexPath.row + 1];
            return cell;
        }
        Model *model = self.ggArray[0];
        UILabel *lable = (UILabel *)[self.view viewWithTag:2000];
        lable.text = model.title;
        UILabel *numberLable =  (UILabel *)[self.view viewWithTag:2001];
        numberLable.text = @"1/4";
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.image]];
        return cell;
    }else if ([collectionView isEqual:self.activitrCol]){
        ACCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:acCell forIndexPath:indexPath];
        if (indexPath.row < 3) {
            Model *model = self.activityArray[indexPath.row];
            cell.myLable.text = model.title;
            return cell;
        }
        Model *model = self.activityArray[0];
        cell.myLable.text = model.title;
        return cell;
    }else{
        ButtonVCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collCELL forIndexPath:indexPath];
        cell.picImage.image = [UIImage imageNamed:self.buttonArray[indexPath.row]];
        cell.lable.text = self.titleArray[indexPath.row];
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionView *coll = [self.view viewWithTag:1010];
    if ([collectionView isEqual:self.ggCollection]){
        LunBoTuView *view = [[LunBoTuView alloc]init];
        view.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:view animated:YES];
        [view release];
    }else if([collectionView isEqual:coll]){
    ButtonView *buttonVC = [[ButtonView alloc]init];
    buttonVC.hidesBottomBarWhenPushed = YES;  //点击隐藏Tabbar
    buttonVC.value = indexPath.row;
    [self.navigationController pushViewController:buttonVC animated:YES];
    [buttonVC release];
    }
}
#pragma mark - tableView协议方法
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:self.squareTableView]) {
        return self.squareArray.count;
    }
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Model *model = self.squareArray[indexPath.row];
    if ([tableView isEqual:self.squareTableView]) {
        if (model.imgUrls.count == 0) {
            NOImageCell *cell = [tableView dequeueReusableCellWithIdentifier:noCell forIndexPath:indexPath];
            cell.model = model;
            return cell;
        }else{
            ThreeImageCell *cell = [tableView dequeueReusableCellWithIdentifier:thirdCell forIndexPath:indexPath];
            cell.model = model;
            return cell;
        }
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
    return cell;
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        Model *model = self.squareArray[indexPath.row];
    if ([tableView isEqual:self.squareTableView]) {
        if (model.imgUrls.count == 0) {
            return 100;
         }else{
             return 210;
        }
    }
    return 0;
}
//点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Model *model = self.squareArray[indexPath.row];
    LunBoTuView *view = [[LunBoTuView alloc]init];
    view.model = model;
    view.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:view animated:YES];
    [view release];
}


































































- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end


//论坛广场http://mrobot.pconline.com.cn/s-900/onlineinfo/bbs/topics.xsp?type=hot_day&forums=769285,587012,769283,768253,250,721485,45,240027,671334,240201,681102,2,48,76,86&singleForum=false&noForums=240022&ie=utf-8&count=20&showImage=true&size=240x180

/**
 思路 根据有没有图片和来确定tableViewCell
 有几张图片
*/
//列表


















